﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using Microsoft.VisualBasic.FileIO;

using MediaLibrary.Controls;

using AMPLibrary;
using AMPLibrary.Collections;
using AMPLibrary.Controllers;
using AMPLibrary.Media;

namespace MediaLibrary
{
    /// <summary>
    /// Interaction logic for VideoPage.xaml
    /// </summary>
    public partial class VideoPage : Page
    {
        VideoCollection videoCollection = VideoCollection.GetVideoCollection();

        WatchFolderController watchFolderController = WatchFolderController.GetWatchFolderController();

        NowPlayingCollection npc = NowPlayingCollection.GetNowPlayingCollection();

        private object defaultNode = null;

        /// <summary>
        /// 
        /// </summary>
        public VideoPage()
        {
            InitializeComponent();

            this.videoTreeView2.SelectedItemChanged += new RoutedPropertyChangedEventHandler<object>(videoTreeView_SelectedItemChanged);
            watchFolderController.PropertyChanged += new System.ComponentModel.PropertyChangedEventHandler(watchFolderController_PropertyChanged);
            watchFolderController.VideoScanComplete += new WatchFolderController.VideoScanCompleteEventHandler(watchFolderController_VideoScanComplete);
            
            BindContent();
        }

        /// <summary>
        /// 
        /// </summary>
        private void Pause()
        {
            watchFolderController.PauseVideo = true;
        }

        /// <summary>
        /// 
        /// </summary>
        private void Unpause()
        {
            watchFolderController.PauseVideo = false;
        }

#region Global

        /// <summary>
        /// 
        /// </summary>
        public void BindContent()
        {
            Pause();

            try
            {
                this.videoListBox.ItemsSource = videoCollection;
                this.videoListBox.Items.Refresh();

                if (this.videoListBox.Items.Count > 0)
                    this.videoListBox.SelectedIndex = 0;
            }
            catch
            {
            }

            try
            {
                this.videoCounterLabel.Content = "Videos: " + this.videoListBox.Items.Count.ToString();
            }
            catch
            {
            }

            BuildTree();

            Unpause();
        }

        /// <summary>
        /// 
        /// </summary>
        private void BuildTree()
        {
            Pause();

            videoTreeView2.Items.Clear();
            videoTreeView2.Items.Refresh();

            foreach (string path in Settings.VideoWatchFolders)
            {
                try
                {
                    if (path != string.Empty)
                    {
                        TreeViewItem item = new TreeViewItem();
                        item.Header = path;
                        item.Tag = path;
                        item.Items.Add(defaultNode);
                        item.Expanded += new RoutedEventHandler(folder_Expanded);

                        videoTreeView2.Items.Add(item);
                    }
                }
                catch
                {
                }
            }

            Unpause();
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        public void refreshButton_Click(object sender, RoutedEventArgs e)
        {
            BindContent();
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        void watchFolderController_PropertyChanged(object sender, System.ComponentModel.PropertyChangedEventArgs e)
        {
            if (e.PropertyName == "CurrentVideoFolder")
                this.currentlyScanning.Content = watchFolderController.CurrentVideoFolder;
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        void watchFolderController_VideoScanComplete(object sender, EventArgs e)
        {
            BindContent();
        } 

#endregion

#region AllVideo

        /// <summary>
        /// 
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void addToNowPlayingAllVideo_OnClick(object sender, RoutedEventArgs e)
        {
            Pause();

            foreach (MediaFile selectedMedia in this.videoListBox.SelectedItems)
                npc.AddItem(selectedMedia);

            Unpause();
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void replaceNowPlayingAllVideo_OnClick(object sender, RoutedEventArgs e)
        {
            Pause();

            npc.ClearItems();

            foreach (MediaFile selectedMedia in this.videoListBox.SelectedItems)
                npc.AddItem(selectedMedia);

            Unpause();
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void addToNewPlaylistAllVideo_OnClick(object sender, RoutedEventArgs e)
        {
            Pause();

            Collection<MediaFile> newPlaylistMedia = new Collection<MediaFile>();

            foreach (MediaFile selectedMedia in this.videoListBox.SelectedItems)
            {
                if (selectedMedia == null)
                    return;

                newPlaylistMedia.AddItem(selectedMedia);
            }

            NewPlaylistWindow npw = new NewPlaylistWindow(newPlaylistMedia);

            if (npw.ShowDialog().Value == true)
            {
                PlaylistCollection playlistCollection = PlaylistCollection.GetPlaylistCollection();
                playlistCollection.AddItem(new Playlist() { Path = npw.PlaylistPath, Title = System.IO.Path.GetFileName(npw.PlaylistPath) });
            }

            Unpause();
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void addToPlaylistAllVideo_OnClick(object sender, RoutedEventArgs e)
        {
            Pause();

            if (videoListBox.SelectedItems == null)
                return;

            PlaylistSelectionWindow psw = new PlaylistSelectionWindow();

            if (psw.ShowDialog().Value == true)
            {
                Playlist playlist = psw.SelectedPlaylist;

                Collection<MediaFile> mediaCollection = new Collection<MediaFile>();

                foreach (MediaFile media in videoListBox.SelectedItems)
                    mediaCollection.AddItem(media);

                Playlist.AddItems(playlist.Path, mediaCollection);
            }

            Unpause();
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void deleteFromLibraryAllVideo_OnClick(object sender, RoutedEventArgs e)
        {
            Pause();

            if (this.videoListBox.SelectedItems == null)
                return;

            if (MessageBox.Show("Delete media from library? (Files will be added to the Excluded Media List)", "Confirm Media Delete", MessageBoxButton.OKCancel) == MessageBoxResult.OK)
            {
                foreach (MediaFile item in this.videoListBox.SelectedItems)
                {
                    Collection<MediaFile> selectedMedia = new Collection<MediaFile>(from media in videoCollection where item.Path == media.Path select media);

                    foreach (MediaFile media in selectedMedia)
                    {
                        videoCollection.RemoveItem(media);
                        ExcludedMediaCollection emc = ExcludedMediaCollection.GetExcludedMediaCollection();
                        emc.AddItem(media.Path);
                    }
                }

                BindContent();
            }

            Unpause();
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void deleteFromLibraryComputerAllVideo_OnClick(object sender, RoutedEventArgs e)
        {
            Pause();

            if (this.videoListBox.SelectedItems == null)
                return;

            if (MessageBox.Show("Delete media from library? (Files will moved to your Recycle Bin)", "Confirm Media Delete", MessageBoxButton.OKCancel) == MessageBoxResult.OK)
            {
                foreach (MediaFile item in videoListBox.SelectedItems)
                {
                    Collection<MediaFile> selectedMedia = new Collection<MediaFile>(from media in videoCollection where item.Path == media.Path select media);

                    foreach (MediaFile media in selectedMedia)
                    {
                        videoCollection.RemoveItem(media);
                        FileSystem.DeleteFile(media.Path, UIOption.OnlyErrorDialogs, RecycleOption.SendToRecycleBin);
                    }
                }

                BindContent();
            }

            Unpause();
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void allVideoListBox_SelectionChanged(object sender, RoutedEventArgs e)
        {

        }

#endregion

#region FolderView

        /// <summary>
        /// 
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        void videoTreeView_SelectedItemChanged(object sender, RoutedPropertyChangedEventArgs<object> e)
        {
            Pause();

            if (videoTreeView2.SelectedItem == null)
                return;

            try
            {
                this.videoListBox2.ItemsSource = (from v in videoCollection where v.Path.Contains(((TreeViewItem)videoTreeView2.SelectedItem).Tag.ToString()) orderby v.Path select v);

                if (this.videoListBox2.Items.Count > 0)
                    this.videoListBox2.SelectedIndex = 0;
            }
            catch
            {
            }

            Unpause();
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        void folder_Expanded(object sender, RoutedEventArgs e)
        {
            Pause();

            TreeViewItem item = (TreeViewItem)sender;
            if (item == null)
                return;

            if (item.Items.Count == 1 && item.Items[0] == defaultNode)
            {
                item.Items.Clear();

                try
                {
                    foreach (string s in Directory.GetDirectories(item.Tag.ToString()))
                    {
                        TreeViewItem subitem = new TreeViewItem();
                        subitem.Header = s.Substring(s.LastIndexOf("\\") + 1);
                        subitem.Tag = s;
                        subitem.FontWeight = FontWeights.Normal;
                        subitem.Items.Add(defaultNode);
                        subitem.Expanded += new RoutedEventHandler(folder_Expanded);

                        item.Items.Add(subitem);
                    }
                }
                catch
                {

                }
            }

            Unpause();
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void addToNowPlayingTreeView2_OnClick(object sender, RoutedEventArgs e)
        {
            Pause();

            TreeViewItem selectedItem = (TreeViewItem)videoTreeView2.SelectedItem;

            if (selectedItem == null)
                return;

            Collection<MediaFile> selectedMedia = new Collection<MediaFile>(from media in videoCollection where media.Path.Contains(selectedItem.Tag.ToString()) orderby media.Path select media);

            foreach (MediaFile selectedTrack in selectedMedia)
                npc.AddItem(selectedTrack);

            Unpause();
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void replaceNowPlayingTreeView2_OnClick(object sender, RoutedEventArgs e)
        {
            Pause();

            TreeViewItem selectedItem = (TreeViewItem)videoTreeView2.SelectedItem;

            if (selectedItem == null)
                return;

            Collection<MediaFile> selectedMedia = new Collection<MediaFile>(from media in videoCollection where media.Path.Contains(selectedItem.Tag.ToString()) orderby media.Path select media);

            npc.ClearItems();

            foreach (MediaFile selectedTrack in selectedMedia)
                npc.AddItem(selectedTrack);

            Unpause();
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void addToNewPlaylistTreeView2_OnClick(object sender, RoutedEventArgs e)
        {
            Pause();

            TreeViewItem selectedItem = (TreeViewItem)videoTreeView2.SelectedItem;

            if (selectedItem == null)
                return;

            Collection<MediaFile> newPlaylistMedia = new Collection<MediaFile>(from media in videoCollection where media.Path.Contains(selectedItem.Tag.ToString()) orderby media.Path select media);
            
            NewPlaylistWindow npw = new NewPlaylistWindow(newPlaylistMedia);

            if (npw.ShowDialog().Value == true)
            {
                PlaylistCollection playlistCollection = PlaylistCollection.GetPlaylistCollection();
                playlistCollection.AddItem(new Playlist() { Path = npw.PlaylistPath, Title = System.IO.Path.GetFileName(npw.PlaylistPath) });
            }

            Unpause();
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void addToPlaylistTreeView2_OnClick(object sender, RoutedEventArgs e)
        {
            Pause();

            TreeViewItem selectedItem = (TreeViewItem)videoTreeView2.SelectedItem;

            if (selectedItem == null)
                return;

            PlaylistSelectionWindow psw = new PlaylistSelectionWindow();

            if (psw.ShowDialog().Value == true)
            {
                Playlist playlist = psw.SelectedPlaylist;
                Collection<MediaFile> mediaCollection = new Collection<MediaFile>(from media in videoCollection where media.Path.Contains(selectedItem.Tag.ToString()) orderby media.Path select media);

                Playlist.AddItems(playlist.Path, mediaCollection);
            }

            Unpause();
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void deleteFromLibraryTreeView2_OnClick(object sender, RoutedEventArgs e)
        {
            Pause();

            TreeViewItem selectedItem = (TreeViewItem)videoTreeView2.SelectedItem;

            if (selectedItem == null)
                return;

            if (MessageBox.Show("Delete media from library? (Files will be added to the Excluded Media List)", "Confirm Media Delete", MessageBoxButton.OKCancel) == MessageBoxResult.OK)
            {
                Collection<MediaFile> mediaCollection = new Collection<MediaFile>(from media in videoCollection where media.Path.Contains(selectedItem.Tag.ToString()) orderby media.Path select media);

                foreach (MediaFile media in mediaCollection)
                {
                    videoCollection.RemoveItem(media);
                    ExcludedMediaCollection emc = ExcludedMediaCollection.GetExcludedMediaCollection();
                    emc.AddItem(media.Path);
                }

                BindContent();
            }

            Unpause();
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void deleteFromLibraryComputerTreeView2_OnClick(object sender, RoutedEventArgs e)
        {
            Pause();

            TreeViewItem selectedItem = (TreeViewItem)videoTreeView2.SelectedItem;

            if (selectedItem == null)
                return;

            if (MessageBox.Show("Delete media from library? (Files will moved to your Recycle Bin)", "Confirm Media Delete", MessageBoxButton.OKCancel) == MessageBoxResult.OK)
            {
                Collection<MediaFile> mediaCollection = new Collection<MediaFile>(from media in videoCollection where media.Path.Contains(selectedItem.Tag.ToString()) orderby media.Path select media);

                foreach (MediaFile media in mediaCollection)
                {
                    videoCollection.RemoveItem(media);
                    FileSystem.DeleteFile(media.Path, UIOption.OnlyErrorDialogs, RecycleOption.SendToRecycleBin);
                }

                BindContent();
            }

            Unpause();
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void addToNowPlayingVideoListBox2_OnClick(object sender, RoutedEventArgs e)
        {
            Pause();

            foreach (MediaFile selectedTrack in this.videoListBox2.SelectedItems)
                npc.AddItem(selectedTrack);

            Unpause();
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void replaceNowPlayingVideoListBox2_OnClick(object sender, RoutedEventArgs e)
        {
            Pause();

            npc.ClearItems();

            foreach (MediaFile selectedTrack in this.videoListBox2.SelectedItems)
                npc.AddItem(selectedTrack);

            Unpause();
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void addToNewPlaylistVideoListBox2_OnClick(object sender, RoutedEventArgs e)
        {
            Pause();

            Collection<MediaFile> newPlaylistMedia = new Collection<MediaFile>();

            foreach (MediaFile selectedTrack in this.videoListBox2.SelectedItems)
            {
                Collection<MediaFile> mediaItems = new Collection<MediaFile>(from media in videoCollection where media.Path == selectedTrack.Path select media);

                foreach (MediaFile item in mediaItems)
                    newPlaylistMedia.AddItem(item);
            }

            NewPlaylistWindow npw = new NewPlaylistWindow(newPlaylistMedia);

            if (npw.ShowDialog().Value == true)
            {
                PlaylistCollection playlistCollection = PlaylistCollection.GetPlaylistCollection();
                playlistCollection.AddItem(new Playlist() { Path = npw.PlaylistPath, Title = System.IO.Path.GetFileName(npw.PlaylistPath) });
            }

            Unpause();
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void addToPlaylistVideoListBox2_OnClick(object sender, RoutedEventArgs e)
        {
            Pause();

            PlaylistSelectionWindow psw = new PlaylistSelectionWindow();

            if (psw.ShowDialog().Value == true)
            {
                Playlist playlist = psw.SelectedPlaylist;
                Collection<MediaFile> mediaCollection = new Collection<MediaFile>();

                foreach (MediaFile selectedTrack in this.videoListBox2.SelectedItems)
                {
                    Collection<MediaFile> mediaItems = new Collection<MediaFile>(from media in videoCollection where media.Path == selectedTrack.Path select media);

                    foreach (MediaFile item in mediaItems)
                        mediaCollection.AddItem(item);
                }

                Playlist.AddItems(playlist.Path, mediaCollection);
            }

            Unpause();
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void deleteFromLibraryVideoListBox2_OnClick(object sender, RoutedEventArgs e)
        {
            Pause();

            if (MessageBox.Show("Delete media from library? (Files will be added to the Excluded Media List)", "Confirm Media Delete", MessageBoxButton.OKCancel) == MessageBoxResult.OK)
            {
                foreach (MediaFile selectedTrack in this.videoListBox2.SelectedItems)
                {
                    Collection<MediaFile> mediaItems = new Collection<MediaFile>(from media in videoCollection where media.Path == selectedTrack.Path select media);

                    foreach (MediaFile media in mediaItems)
                    {
                        videoCollection.RemoveItem(media);
                        ExcludedMediaCollection emc = ExcludedMediaCollection.GetExcludedMediaCollection();
                        emc.AddItem(media.Path);
                    }
                }

                BindContent();
            }

            Unpause();
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void deleteFromLibraryComputerVideoListBox2_OnClick(object sender, RoutedEventArgs e)
        {
            Pause();

            if (MessageBox.Show("Delete media from library? (Files will moved to your Recycle Bin)", "Confirm Media Delete", MessageBoxButton.OKCancel) == MessageBoxResult.OK)
            {
                foreach (MediaFile selectedTrack in this.videoListBox2.SelectedItems)
                {
                    Collection<MediaFile> mediaItems = new Collection<MediaFile>(from media in videoCollection where media.Path == selectedTrack.Path select media);

                    foreach (MediaFile media in mediaItems)
                    {
                        videoCollection.RemoveItem(media);
                        FileSystem.DeleteFile(media.Path, UIOption.OnlyErrorDialogs, RecycleOption.SendToRecycleBin);
                    }
                }

                BindContent();
            }

            Unpause();
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void allVideoListBox2_SelectionChanged(object sender, RoutedEventArgs e)
        {
        }

#endregion
    }
}
